package com.hippo.core.ui.compose.home.add

import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.hippo.core.R
import com.hippo.core.core.ext.click
import com.hippo.core.ui.components.BottomSheetDialog
import com.hippo.core.ui.components.InputEdit
import com.hippo.core.ui.components.InputEdit2
import com.hippo.core.ui.components.ThemeButton
import com.hippo.core.ui.theme.ColorTextBlack
import com.hippo.core.ui.theme.HippoTheme
import com.hippo.core.utils.AppUtil
import com.hippo.core.utils.UiHandler
import com.thingclips.smart.android.ble.api.ScanDeviceBean

@Composable
fun ConfigNetworkDialog(
    onDismissRequest: () -> Unit,
    onNext: (String,String) -> Unit,
    toHelp: () -> Unit
) {

    val context = LocalContext.current

    var wifi by remember {
        mutableStateOf("")
    }

    LaunchedEffect(Unit) {
        wifi = AppUtil.getConnectedWifiName(context).orEmpty()
    }

    BottomSheetDialog(
        title = stringResource(id = R.string.home_6),
        onDismissRequest = onDismissRequest,
        skipPartiallyExpanded = true,
    ) {
        DialogContent( wifi, onNext,toHelp)
    }

}


@Composable
private fun DialogContent(
    wifi: String,
    onNext: (String,String) -> Unit,
    toHelp: () -> Unit
) {

    var pwd by remember { mutableStateOf("") }

//    val ssid = AppUtil.getConnectedWifiSSID(LocalContext.current).orEmpty()
    val ssid = "sss"

    Column(
        modifier = Modifier
            .padding(16.dp)
            .verticalScroll(rememberScrollState()),
    ) {

        Text(
            text = stringResource(R.string.home_12),
            color = Color.Black,
            fontSize = 25.sp,
        )
        Spacer(modifier = Modifier.height(10.dp))

        Text(
            text = stringResource(R.string.home_13),
            color = Color.Gray,
        )
        Spacer(modifier = Modifier.height(10.dp))

        Text(
            text = stringResource(R.string.home_14),
            color = MaterialTheme.colorScheme.primary,
            textDecoration = TextDecoration.Underline,
            modifier = Modifier.click {
                toHelp()
            }
        )

        Spacer(modifier = Modifier.height(10.dp))

        Image(
            painter = painterResource(R.mipmap.wifi_temp),
            contentDescription = null,
            modifier = Modifier.fillMaxWidth()
        )

        Spacer(modifier = Modifier.height(10.dp))

        Row(
            verticalAlignment = Alignment.CenterVertically
        ) {
            Image(
                painter = painterResource(R.mipmap.wifi_n),
                contentDescription = null,
                modifier = Modifier.size(25.dp)
            )
            Text(
                text = wifi,
                modifier = Modifier.padding(start = 30.dp),
                style = TextStyle(
                    color = ColorTextBlack,
                    fontSize = 14.sp
                )
            )
        }

        Spacer(modifier = Modifier.height(16.dp))

        Row(
            verticalAlignment = Alignment.CenterVertically
        ) {
            Image(
                painter = painterResource(R.mipmap.pwd_2),
                contentDescription = null,
                modifier = Modifier.size(25.dp)
            )

            Spacer(modifier = Modifier.width(16.dp))

            InputEdit2(
                text = pwd,
                hint = stringResource(R.string.login_3),
                modifier = Modifier.height(56.dp),
                keyboardOptions = KeyboardOptions.Default.copy(
                    keyboardType = KeyboardType.Password
                ),
                onValueChange = {
                    pwd = it
                }
            )
        }

        Spacer(modifier = Modifier.height(36.dp))


        ThemeButton(
            text = stringResource(R.string.next)
        ) {
            if(pwd.isNullOrBlank()){
                UiHandler.showToast(R.string.login_7)
            }else{
                onNext(ssid,pwd)
            }
        }

    }

}


@Preview(showBackground = true)
@Composable
private fun DialogContentPreview() {
    HippoTheme {
        DialogContent(
            wifi = "wifi",
            onNext = { _,_->

            },
            toHelp = {}
        )
    }
}




